<p class="Paragraph"><help:paragraphinfo state="U" number="2"/>The following describes the basic use of procedures and functions in <help:productname>%PRODUCTNAME</help:productname> Basic.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="3" xmlns:help="http://openoffice.org/2000/help"/>Procedures (SUBS) and functions (FUNCTIONS) help you maintaining a structured overview by separating a program into logical pieces.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="4" xmlns:help="http://openoffice.org/2000/help"/>One benefit of procedures and functions is that once you have developed a program code containing task components, you can use this code in other project.</p>
<p class="Head2"><help:paragraphinfo state="U" number="26" xmlns:help="http://openoffice.org/2000/help"/><help:key-word value="variables; passing to procedures and functions" tag="kw66513_10" xmlns:help="http://openoffice.org/2000/help"/><help:key-word value="parameters; for procedures and functions" tag="kw66513_9" xmlns:help="http://openoffice.org/2000/help"/><help:key-word value="procedures; parameters" tag="kw66513_8" xmlns:help="http://openoffice.org/2000/help"/><help:key-word value="functions; parameters" tag="kw66513_7" xmlns:help="http://openoffice.org/2000/help"/>Passing Variables to Procedures (SUB) and Functions (FUNCTION)</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="27" xmlns:help="http://openoffice.org/2000/help"/>Variables can be passed to both procedures and functions. The SUB or FUNCTION must be declared to expect parameters:</p>
<p class="PropText"><help:paragraphinfo state="U" number="28" xmlns:help="http://openoffice.org/2000/help"/>SUB SubName(<span class="T1">Parameter1 As Type, Parameter2 As Type,...</span>)</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="31" xmlns:help="http://openoffice.org/2000/help"/>The SUB is called using the following syntax:</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="33" xmlns:help="http://openoffice.org/2000/help"/>The parameters passed to a SUB must fit to those specified in the SUB declaration.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="34" xmlns:help="http://openoffice.org/2000/help"/>The same process applies to FUNCTIONS. In addition, functions always return a function result. The result of a function is defined by assigning the return value to the function name:</p>
<p class="PropText"><help:paragraphinfo state="U" number="35" xmlns:help="http://openoffice.org/2000/help"/>FUNCTION FunctionName(Parameter1 As Type, Parameter2 As Type,...) As Type</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="39" xmlns:help="http://openoffice.org/2000/help"/>The FUNCTION is called using the following syntax:</p>
<p class="Head2"><help:paragraphinfo state="U" number="45" xmlns:help="http://openoffice.org/2000/help"/><help:key-word value="parameters; pass by reference" tag="kw66513_6" xmlns:help="http://openoffice.org/2000/help"/><help:key-word value="parameters; pass by value" tag="kw66513_5" xmlns:help="http://openoffice.org/2000/help"/>Passing Variables by Value or Reference</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="47" xmlns:help="http://openoffice.org/2000/help"/>Parameters can be passed to a SUB or a FUNCTION either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a SUB or a FUNCTION gets the parameter and can read and modify its value.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="53" xmlns:help="http://openoffice.org/2000/help"/>If you want to pass a parameter by value insert the key word "ByVal" in front of the parameter when you call a SUB or FUNCTION, for example:</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="55" xmlns:help="http://openoffice.org/2000/help"/>In this case, the original content of the parameter will not be modified by the FUNCTION since it only gets the value and not the parameter itself.</p>
<p class="TextInTable"><help:paragraphinfo state="U" number="56" xmlns:help="http://openoffice.org/2000/help"/>When you create a new module, <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic automatically inserts a SUB called "Main". This default name has nothing to do with the order or the starting point of a <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic project. You can also safely rename this SUB.</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="58" xmlns:help="http://openoffice.org/2000/help"/>A variable defined within a SUB or FUNCTION, only remains valid until the procedure is exited. This is known as a "local" variable. In many cases, you need a variable to be valid in all procedures, in every module of all libraries, or after a SUB or FUNCTION is exited. This property is controlled in a variable declaration through key words in the <span class="T1">Dim</span> statement.</p>
<p class="Head3"><help:paragraphinfo state="U" number="59" xmlns:help="http://openoffice.org/2000/help"/>Declaring Variables Outside a SUB or FUNCTION</p>
<p class="PropText"><help:paragraphinfo state="U" number="111" xmlns:help="http://openoffice.org/2000/help"/>DIM GLOBAL VarName As TYPENAME</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="112" xmlns:help="http://openoffice.org/2000/help"/>The variable is valid as long as the <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> session lasts.</p>
<p class="PropText"><help:paragraphinfo state="U" number="60" xmlns:help="http://openoffice.org/2000/help"/>DIM PUBLIC VarName As TYPENAME</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="61" xmlns:help="http://openoffice.org/2000/help"/>The variable is valid in all modules.</p>
<p class="PropText"><help:paragraphinfo state="U" number="62" xmlns:help="http://openoffice.org/2000/help"/>DIM PRIVATE VarName As TYPENAME</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="63" xmlns:help="http://openoffice.org/2000/help"/>The variable is only valid in this module.</p>
<p class="PropText"><help:paragraphinfo state="U" number="64" xmlns:help="http://openoffice.org/2000/help"/>DIM VarName As TYPENAME</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="65" xmlns:help="http://openoffice.org/2000/help"/>The variable is only valid in this module.</p>
<p class="Head3"><help:paragraphinfo state="U" number="66" xmlns:help="http://openoffice.org/2000/help"/>Saving Variable Content after Exiting a SUB or FUNCTION</p>
<p class="PropText"><help:paragraphinfo state="U" number="67" xmlns:help="http://openoffice.org/2000/help"/>DIM STATIC VarName As TYPENAME</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="68" xmlns:help="http://openoffice.org/2000/help"/>The variable retains its value until the next time the FUNCTION or SUB is entered. The declaration must exist inseide a SUB or a FUNCTION.</p>
<p class="Head2"><help:paragraphinfo state="U" number="41" xmlns:help="http://openoffice.org/2000/help"/><help:key-word value="functions; return value type" tag="kw66513_2" xmlns:help="http://openoffice.org/2000/help"/>Specifying the Return Value Type of a FUNCTION</p>
<p class="Paragraph"><help:paragraphinfo state="U" number="42" xmlns:help="http://openoffice.org/2000/help"/>As with variables, include a type-declaration character after the function name, or the type indicated by "As" and the corresponding key word at the end of the parameter list to define the type of the function's return value, for example:</p>